package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.mongo.FocusUser;
import com.itheima.domain.mongo.Video;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.VideoVo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.FocusUserService;
import com.itheima.service.mongo.VideoService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 请不要叫我高司令
 * @Date 2022/3/23 18:36
 * @Version 1.0
 */

@Component
public class VideoManager {


    @DubboReference
    private VideoService videoService;
    @DubboReference
    private UserInfoService userInfoService;

    @Autowired
    private FastFileStorageClient client;

    @Autowired
    private FdfsWebServer webServer;

    @DubboReference
    private FocusUserService focusUserService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;





    //推荐视频查询
    public ResponseEntity findVideoVoByPage(Integer pageNum, Integer pageSize) {

        Long userId = UserHolder.get().getId();

        //rpc查询推荐视频分页对象
        PageBeanVo pageBeanVo = videoService.findVideoByPage(userId, pageNum, pageSize);
        ArrayList<VideoVo> voList = new ArrayList<>();

        //获取视频详情列表，遍历封装vo对象
        List<Video> videoList = (List<Video>) pageBeanVo.getItems();

        if (CollectionUtil.isNotEmpty(videoList)) {

            for (Video video : videoList) {

                VideoVo vo = new VideoVo();
                vo.setUserInfo(userInfoService.findById(video.getUserId()));
                vo.setVideo(video);
                if (stringRedisTemplate.hasKey(StrUtil.format(ConstantUtil.FOCUS_USER, userId, vo.getUserId()))) {

                    vo.setHasFocus(1);

                }

                voList.add(vo);
            }

        }

        //设置分页对象
        pageBeanVo.setItems(voList);

        //返回分页对象
        return ResponseEntity.ok(pageBeanVo);


    }

    //保存小视频
    public void saveVideo(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {

        Long userId = UserHolder.get().getId();

        //上传封面
        StorePath storePath1 = client.uploadFile(
                videoThumbnail.getInputStream(),
                videoThumbnail.getSize(),
                FileUtil.extName(videoThumbnail.getOriginalFilename()),
                null);

        String picUrl = webServer.getWebServerUrl() + storePath1.getFullPath();

        //上传视频
        StorePath storePath2 = client.uploadFile(videoFile.getInputStream(),
                videoFile.getSize(),
                FileUtil.extName(videoFile.getOriginalFilename()),
                null);
        String videoUrl = webServer.getWebServerUrl() + storePath2.getFullPath();


        //封装video对象
        Video video = new Video();

        video.setId(ObjectId.get()); // 视频主键
        video.setCreated(System.currentTimeMillis());// 发布时间
        video.setUserId(userId);// 视频发布人
        video.setText("小鸡炖蘑菇"); // 视频文字
        video.setPicUrl(picUrl);// 视频封面
        video.setVideoUrl(videoUrl); // 视频内容
        //rpc保存
        videoService.saveVideo(video);


    }

    //关注视频作者
    public void userFocus(Long focusUserId) {

        Long userId = UserHolder.get().getId();

        //构建FocusUser对象
        FocusUser focusUser = new FocusUser();

        focusUser.setUserId(userId);
        focusUser.setFocusUserId(focusUserId);
        focusUser.setCreated(System.currentTimeMillis());

        //rpc调用保存
        focusUserService.saveFocusUser(focusUser);

        //在redis上保存关注标记
        stringRedisTemplate.opsForValue().set(StrUtil.format(ConstantUtil.FOCUS_USER, userId, focusUserId), "1");


    }

    //取消关注视频作者
    public void userUnFocus(Long focusUserId) {

        Long userId = UserHolder.get().getId();

        focusUserService.removeFocusUser(userId, focusUserId);

        //redis中删除标记
        stringRedisTemplate.delete(StrUtil.format(ConstantUtil.FOCUS_USER, userId, focusUserId));


    }
}



















